package router

import (
	"github.com/e421083458/gin_scaffold/controller"
	"github.com/e421083458/gin_scaffold/middleware"
	"github.com/gin-gonic/contrib/sessions"
	"github.com/gin-gonic/gin"
	"github.com/swaggo/files"
	"github.com/swaggo/gin-swagger"
)

func InitRouter(middlewares ...gin.HandlerFunc) *gin.Engine {

	router := gin.Default()
	router.Use(middlewares...)
	store := sessions.NewCookieStore([]byte("secret"))
	router.GET("/ping", func(c *gin.Context) {
		c.JSON(200, gin.H{
			"message": "pong",
		})
	})
	router.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler))

	//demo sessions.Sessions("mysession", store) 开启路由组session
	v1 := router.Group("/demo")
	v1.Use(sessions.Sessions("mysession", store), middleware.RecoveryMiddleware(), middleware.RequestLog(), middleware.IPAuthMiddleware(), middleware.TranslationMiddleware())
	{
		controller.DemoRegister(v1)
	}
	//车机处理
	//moto := router.Group("/moto")
	//moto.Use(
	//	sessions.Sessions("mysession", store),
	//	middleware.RecoveryMiddleware(),
	//	middleware.RequestLog(),
	//	middleware.IPAuthMiddleware(),
	//	middleware.TranslationMiddleware(),
	//	//middleware.AuthMiddleware(),
	//)
	//{
	//	controller.MotoRegister(moto)
	//}
	//会员相关
	//非登陆接口
	apiNormalGroup := router.Group("/api")
	apiNormalGroup.Use(
		sessions.Sessions("mysession", store),
		middleware.RecoveryMiddleware(),
		middleware.RequestLog(),
		middleware.TranslationMiddleware())
	{
		controller.ApiRegister(apiNormalGroup)
	}
	//登陆接口
	apiAuthGroup := router.Group("/api")
	apiAuthGroup.Use(
		sessions.Sessions("mysession", store),
		middleware.RecoveryMiddleware(),
		middleware.RequestLog(),
		middleware.TranslationMiddleware(),
		middleware.JWT(),
	)
	{
		controller.ApiLoginRegister(apiAuthGroup)

	}

	//订单
	//非登陆接口
	orderNormalGroup := router.Group("/order")
	orderNormalGroup.Use(
		sessions.Sessions("mysession", store),
		middleware.RecoveryMiddleware(),
		middleware.RequestLog(),
		middleware.TranslationMiddleware())
	{
		controller.OrderNoLogin(orderNormalGroup)
	}
	//登陆接口
	orderAuthGroup := router.Group("/order")
	orderAuthGroup.Use(
		sessions.Sessions("mysession", store),
		middleware.RecoveryMiddleware(),
		middleware.RequestLog(),
		middleware.TranslationMiddleware(),
		middleware.JWT(),
	)
	{
		controller.OrderLogin(orderAuthGroup)

	}

	return router
}
